﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Win32;

namespace Dashboard
{
    public partial class UC_SerialPortl : UserControl
    {

        IniFiles ini;

        public UC_SerialPortl()
        {
            InitializeComponent();

        }


        private void UC_SerialPortl_Load(object sender, EventArgs e)
        {
            List<String> comList = GetComlist(false);
            for (int i = 0; i < comList.Count; i++)
                this.comboBox1.Items.Add(comList[i]);
            this.comboBox2.Items.Add("9600");

            ini = new IniFiles("setting.ini");
            this.comboBox1.Text = ini.ReadString("Serial", "com", "");
            this.comboBox2.Text = ini.ReadString("Serial", "baudRate", "");



        }

        private List<string> GetComlist(bool isUseReg)
        {
            List<string> list = new List<string>();
            try
            {
                if (isUseReg)
                {
                    RegistryKey RootKey = Registry.LocalMachine;
                    RegistryKey Comkey = RootKey.OpenSubKey(@"HARDWARE\DEVICEMAP\SERIALCOMM");

                    String[] ComNames = Comkey.GetValueNames();

                    foreach (String ComNamekey in ComNames)
                    {
                        string TemS = Comkey.GetValue(ComNamekey).ToString();
                        list.Add(TemS);
                    }
                }
                else
                {
                    foreach (string com in System.IO.Ports.SerialPort.GetPortNames())  //自动获取串行口名称
                        list.Add(com);
                }
            }
            catch
            {
            }
            return list;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ini.WriteString("Serial", "com", this.comboBox1.Text);
            ini.WriteString("Serial", "baudRate", this.comboBox2.Text);
        }
    }
}
